<template>
	<xn-form-container
		:title="formData.id ? '新增新闻' : '编辑新闻'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item label="栏目：" name="columnName">
						<a-input v-model:value="formData.columnName" placeholder="请输入栏目" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="标题：" name="title">
						<a-input v-model:value="formData.title" placeholder="请输入标题" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="来源：" name="sourceName">
						<a-input v-model:value="formData.sourceName" placeholder="请输入简介" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="是否置顶：" name="needTop">
						<a-radio-group v-model:value="formData.needTop" :options="needTopOptions"></a-radio-group>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="简介：" name="synopsis">
						<a-textarea
							v-model:value="formData.synopsis"
							placeholder="请输入简介"
							:auto-size="{ minRows: 2, maxRows: 5 }"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="内容：" name="content">
						<xn-editor v-model="formData.content" placeholder="请输入内容"/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button style="margin-right: 8px" type="primary" @click="onSubmit(0)">保存</a-button>
			<a-button type="warn" @click="onSubmit(1)" :loading="submitLoading">发布</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="newsInfoForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import newsInfoApi from '@/api/biz/notice/newsInfoApi'
	import tool from "@/utils/tool"
	import XnEditor from '@/components/Editor/index.vue'
	import bizOrgApi from "@/api/biz/bizOrgApi";

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const needTopOptions = tool.dictList('top_status')
	const showOrgVisible = ref(false)
	// 定义机构元素
	const treeData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}


	// 默认要校验的
	const formRules = {
		columnName: [required('请输入栏目')],
		title: [required('请输入标题')],
		type: [required('请选择类型')],
		sourceName: [required('请输入来源')],
		orgIds: [required('请选择发送单位')],
		needTop: [required('请选择是否置顶')],
		content: [required('请输入内容')],
	}
	// 验证并提交数据
	const onSubmit = (status) => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			formData.value.status = status
			const formDataParam = cloneDeep(formData.value)
			newsInfoApi
				.newsInfoSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
